/* Just a doodle so far... */

#include <ft2build.h>
#include FT_FREETYPE_H
#include <stdio.h>
#include <stdbool.h>

static  char test_input[100] = "1234567890.,? ABCEDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";



int prop_moveto (const FT_Vector * to, void * user)
{
  printf( " M %d %d;", to->x, to->y );
  return 0;
}


int prop_lineto (const FT_Vector * to, void * user)
{
  printf( " L %d %d;", to->x, to->y );
  return 0;
}


int prop_conicto (const FT_Vector * cp1, const FT_Vector * to, void * user)
{
  printf( " CN %d %d %d %d;", 
          cp1->x, cp1->y, to->x, to->y );
  return 0;
}

int prop_curveto (const FT_Vector * cp1, const FT_Vector * cp2, 
                const FT_Vector * to, void * user)
{

  printf( " C %d %d %d %d %d %d;", 
          cp1->x, cp1->y, cp2->x, cp2->y, to->x, to->y );
  return 0;
}

int main (int argc, char ** argv )
{
  FT_Error err;
  FT_Library ft_lib;
  FT_Face ft_face;
  
  FT_String * ft_str;
  FT_UInt glyph_idx;
  FT_GlyphSlot slot;

  FT_Vector cpoint;
  FT_Fixed advh;

  FT_Outline_Funcs funcs;

  FT_UInt i,j;

  const char * font_name;
  
  char * path_to_font;


  if (argc !=2)
  {
    printf("Usage: ft_props path_to_font\n");
    printf("argc = %d\n", argc) ;
    exit (1);
  }

  path_to_font = argv[1];



  err = FT_Init_FreeType(&ft_lib);
  if (err != 0)
  {
    fprintf(stderr, "FT_Init_FreeType - err_no %d \n", err);
    exit(err);
  }
  
  err = FT_New_Face(ft_lib, path_to_font, 0, &ft_face);
  if (err !=0) 
  {
    fprintf(stderr, "FT_New_Face - err_no %d \n", err);
    FT_Done_FreeType(ft_lib);
    exit(err);
  }


  printf("StartFontProps 1.0\n");

  font_name = FT_Get_Postscript_Name(ft_face);

  printf("FontName %s\n", font_name);
  printf("FamilyName %s\n", ft_face->family_name);
  printf("StyleName %s\n", ft_face->style_name);


  printf( "FontBBox %d %d %d %d\n", 
          ft_face->bbox.xMin,
          ft_face->bbox.yMin,
          ft_face->bbox.xMax,
          ft_face->bbox.yMax );

  printf("UnitsPerEM %d\n", ft_face->units_per_EM);
  printf("Ascender %d\n", ft_face->ascender);
  printf("Descender %d\n", ft_face->descender);
  printf("Height %d\n", ft_face->height);


  /*  printf("%% Num faces: %d\n",   ft_face->num_faces); */


  

  err = FT_Set_Char_Size(ft_face, 9, 0, 600, 600);
  if (err != 0) 
  {
    fprintf(stderr, "FT_Set_Char_Size - err_no %d \n", err);
  }

  slot = ft_face->glyph;

  printf("StartCharMetrics\n");

  for(i=0; i < strlen(test_input); i++)
  {

    printf ( "N %c;", test_input[i]);

    err = FT_Load_Char(ft_face, test_input[i], FT_LOAD_DEFAULT);
    if (err != 0)
    { 
      fprintf(stderr, "FT_Load_Char - err_no %d \n", err);
    }

    printf(" W %d; H %d;", slot->metrics.width, slot->metrics.height );

    printf(" AX %d; AY %d;", slot->advance.x, slot->advance.y );

    printf("\n");

  }
  
  printf("EndCharMetrics\n");



  /* use FT_Outline_Decompose */

  funcs.move_to = (FT_Outline_MoveToFunc)&prop_moveto;
  funcs.line_to = (FT_Outline_LineToFunc)&prop_lineto;
  funcs.conic_to = (FT_Outline_ConicToFunc)&prop_conicto;
  funcs.cubic_to = (FT_Outline_CubicToFunc)&prop_curveto;
  funcs.shift = 0;
  funcs.delta = 0;


  printf("StartCharPaths\n");

  for(i=0; i < strlen(test_input); i++)
  {

    printf ( "N %c;", test_input[i]);

    err = FT_Load_Char(ft_face, test_input[i], FT_LOAD_DEFAULT);
    if (err != 0) 
    { 
      fprintf(stderr, "%% err_no %d \n", err);
    }

    err =  FT_Outline_Decompose( &(slot->outline), &funcs, NULL);
    if (err != 0) 
    {
      fprintf(stderr, "%% err_no %d \n", err);
    }

    printf("\n");

  }
  
  printf("EndCharPaths\n");
  printf("EndFontProps\n");
  
  err = FT_Done_Face(ft_face);
  if (err != 0) 
  { 
    fprintf(stderr, "%% err_no %d \n", err);
  }

  err = FT_Done_FreeType(ft_lib);
  if (err != 0) 
  { 
    fprintf(stderr, "%% err_no %d \n", err);
  }
  
  return 0;  
}  


